package com.code;

/**
 * Created by lucky on 2018/10/26.
 */
public class Code11 {

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x; }
    }

    public static boolean isValidBST(TreeNode root) {
        if(root == null){
            return true;
        }
        if(root.left == null && root.right== null
                || root.val > root.left.val && root.val < root.right.val){
            return isValidBST(root.left) && isValidBST(root.right);
        }else{
            return false;
        }
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(5);
        TreeNode root1 = new TreeNode(1);
        TreeNode root2 = new TreeNode(4);
        TreeNode root3 = new TreeNode(3);
        TreeNode root4 = new TreeNode(6);
        root.left = root1;
        root.right = root2;
        root.right.left = root3;
        root.right.right = root4;
        boolean validBST = isValidBST(root);
        System.out.println(validBST);
    }
}
